package cn.wolfcode.p2p.base.service.impl;
import java.util.Date;

import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.mapper.VideoAuthMapper;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.base.service.IVideoAuthService;

import cn.wolfcode.p2p.base.domain.VideoAuth;
import java.util.List;
import cn.wolfcode.base.QueryObject;
import cn.wolfcode.p2p.base.util.BitStateUtil;
import cn.wolfcode.p2p.base.util.DateUtil;
import cn.wolfcode.p2p.base.util.LoginInfoContext;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class VideoAuthServiceImpl implements IVideoAuthService {
    @Autowired
    private VideoAuthMapper videoAuthMapper;
    @Autowired
	private IUserInfoService userInfoService;
	
	@Transactional(readOnly=true)
	public PageInfo<VideoAuth> query(QueryObject qo) {
		PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
		List<VideoAuth> list = videoAuthMapper.selectList(qo);
		return new PageInfo<VideoAuth>(list);
	}

	@Override
	public VideoAuth get(Long videoAuthId) {
		return videoAuthMapper.selectById(videoAuthId);
	}

	@Override
	public void applyVideAudit(String appointmentDate, String appointmentTime, Long customServiceId) {
		// 获取当前登录对象的用户信息
		UserInfo userInfo = userInfoService.get(LoginInfoContext.getCurrentLoginInfo().getId());
		// 判断该用户信息是否存在视频认证id
		if (userInfo.getVideoAuthId() == null){
			// 拆分时间段
			String[] arr = appointmentTime.split("-");
			if (arr == null || arr.length != 2){
				return;
			}
			String begin = appointmentDate + " " + arr[0];
			String end = appointmentDate + " " + arr[1];

			Date beginDate = DateUtil.string2date(begin, "yyyy-MM-dd HH:mm");
			Date endDate = DateUtil.string2date(end, "yyyy-MM-dd HH:mm");

			// 创建视频认证的对象
			VideoAuth videoAuth = new VideoAuth();
			LoginInfo auditor = new LoginInfo();
			auditor.setId(customServiceId);

			videoAuth.setState(VideoAuth.STATE_WAIT)
					.setApplyTime(new Date())
					.setAuditor(auditor)
					.setApplier(LoginInfoContext.getCurrentLoginInfo());

			videoAuth.setBeginAppointmentTime(beginDate);
			videoAuth.setEndAppointmentTime(endDate);

			videoAuthMapper.insert(videoAuth);

			// 在用户信息上添加视频认证信息id
			userInfo.setVideoAuthId(videoAuth.getId());
			userInfoService.update(userInfo);
		}

	}

    @Override
    public void auditVideoAuth(Long videoAuthId, Integer state, String remark) {
        LoginInfo current = LoginInfoContext.getCurrentLoginInfo();
        // 判断是否是待审核
        VideoAuth videoAuth = videoAuthMapper.selectById(videoAuthId);
        if (videoAuth.getState() == VideoAuth.STATE_WAIT) {
            // 判断该用户是否已经完成审核
            UserInfo userInfo = userInfoService.get(videoAuth.getApplier().getId());
            if (!userInfo.getHasVideoAuth()){
                // 判断当前登录对象是否是指定审核人或者是超级管理员
                if (current.getId() == videoAuth.getAuditor().getId() || current.isAdmin()) {
                    // 保存审核信息
                    videoAuth.setState(state)
                            .setRemark(remark)
                            .setAuditTime(new Date());

                    videoAuthMapper.updateById(videoAuth);

                    // 根据审核的状态修改用户信息的视频认证
                    if (videoAuth.getState() == VideoAuth.STATE_PASS){
                        userInfo.addNewState(BitStateUtil.OP_VIDEO_AUTH);
                    }else {
                        userInfo.setVideoAuthId(null);
                    }
                    userInfoService.update(userInfo);
                }
            }
        }
    }
}
